const common = require('../../../mixins/common')
const shop = require('../../../mixins/shop')
var util = require('../../../utils/util.js');
var app = getApp();
const options = {

  /**
   * 页面的初始数据
   */
  data: {
    navbarTrans: 0,
    color: "0,0,0",
    iconTheme: "white",
    iconLeft: "/image/search.png",
    iconShop: "/image/workorder.png",
    banner: [],
    notice: [],
    plateList: [],
    topload: true,
    swiperList: [],
    hotList: [],
    recommendList: [],
    page: 1,
    loadmoreShow: false,
    isLastPage: false,
    vipPopup: false,
    cartCount: 0,
    shopPopup: false,
  },

  onPageScroll: function onPageScroll(e) {
    let that = this;
    var trans = (e.scrollTop > 55 ? 55 : e.scrollTop) / 55;
    var frontColor = "#ffffff";
    var backgroundColor = "#000000";
    var iconTheme = that.data.iconTheme;
    var iconLeft = that.data.iconLeft;
    var iconShop = that.data.iconShop;
    if (trans <= 0) {
      frontColor = "#ffffff";
      backgroundColor = "#000000";
      iconTheme = "white";
      iconLeft = "/image/search.png";
      iconShop = "/image/workorder.png";
    } else if (trans >= 0.4) {
      frontColor = "#000000";
      backgroundColor = "#ffffff";
      iconTheme = "black";
      iconLeft = "/image/search-line.png";
      iconShop = "/image/workorder-o.png";
    }
    that.setData({
      navbarTrans: trans,
      iconTheme: iconTheme,
      iconLeft: iconLeft,
      iconShop: iconShop,
    });
    wx.setNavigationBarColor({
      frontColor: frontColor,
      backgroundColor: frontColor,
      animation: {
        duration: 400,
        timingFunc: 'easeIn'
      }
    })
  },

  toOrder() {
    wx.navigateTo({
      url: '/pages/shop/order/order',
    })
  },

  toCart() {
    wx.navigateTo({
      url: '/pages/shop/cart/cart',
    })
  },

  bannerTap(e) {
    let id = e.currentTarget.dataset.id;
    if (id) {
      wx.navigateTo({
        url: '/pages/shop/goods-details/goods-details?id=' + id,
      })
    }
  },

  toGoodsClassify(e) {
    let id = e.currentTarget.dataset.id;
    let focus = e.currentTarget.dataset.focus;
    wx.navigateTo({
      url: '/pages/shop/goods-classify/goods-classify?focusShow=' + focus + '&id=' + id,
    })
  },

  toGoodsDetails(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/shop/goods-details/goods-details?id=' + id,
    })
  },

  toNtice(e) {
    wx.navigateTo({
      url: '/pages/clause/clause?id=' + e.currentTarget.dataset.id + '&type=-1'
    })
  },

  onLoad: function () {
    this.getShopBanner();
    this.getShopNotice();
    this.getShopPlate();
    this.getShopInspiration();
    this.getShopHotList();
    this.getShopIndexRecommend();
  },

  onShow: function () {
    "function" == typeof this.getTabBar && this.getTabBar() && this.getTabBar().setData({
      selected: 3
    })
    let that = this;
    that.getSysMessageCount().then(function (res) {
      that.getTabBar().setData({
        sysMessageCount: res
      })
    })
    let res = util.loginNow();
    if (res == true) {
      let userInfo = wx.getStorageSync('userInfo')
      if (!userInfo.is_member) {
        let d = new Date(),
          str = '';
        str += d.getFullYear() + '-';
        str += d.getMonth() + 2 + '-';
        str += d.getDate();
        that.setData({
          tomorrowTime: str,
          shopPopup: true
        })
      } else {
        that.userCartCount();
      }
    }
  },

  toMembers() {
    wx.navigateTo({
      url: '/pages/mine/members/members',
    })
  },

  onVipPopup() {
    this.setData({
      shopPopup: false,
      vipPopup: true,
    })
  },

  vipPopupTap() {
    let that = this;
    let userInfo = wx.getStorageSync('userInfo')
    if (!userInfo.is_member) {
      that.setData({
        vipPopup: false,
        shopPopup: true,
      })
    }
  },

  // 初始化towerSwiper
  towerSwiper(name) {
    let list = this.data[name];
    for (let i = 0; i < list.length; i++) {
      list[i].zIndex = parseInt(list.length / 2) + 1 - Math.abs(i - parseInt(list.length / 2))
      list[i].mLeft = i - parseInt(list.length / 2)
    }
    this.setData({
      swiperList: list
    })
  },
  // towerSwiper触摸开始
  towerStart(e) {
    this.setData({
      towerStart: e.touches[0].pageX
    })
  },
  // towerSwiper计算方向
  towerMove(e) {
    this.setData({
      direction: e.touches[0].pageX - this.data.towerStart > 0 ? 'right' : 'left'
    })
  },
  // towerSwiper计算滚动
  towerEnd() {
    let direction = this.data.direction;
    let list = this.data.swiperList;
    if (direction == 'right') {
      let mLeft = list[0].mLeft;
      let zIndex = list[0].zIndex;
      for (let i = 1; i < list.length; i++) {
        list[i - 1].mLeft = list[i].mLeft
        list[i - 1].zIndex = list[i].zIndex
      }
      list[list.length - 1].mLeft = mLeft;
      list[list.length - 1].zIndex = zIndex;
      this.setData({
        swiperList: list
      })
    } else {
      let mLeft = list[list.length - 1].mLeft;
      let zIndex = list[list.length - 1].zIndex;
      for (let i = list.length - 1; i > 0; i--) {
        list[i].mLeft = list[i - 1].mLeft
        list[i].zIndex = list[i - 1].zIndex
      }
      list[0].mLeft = mLeft;
      list[0].zIndex = zIndex;
      this.setData({
        swiperList: list
      })
    }
  },

  onPullDownRefresh: function () {
    let that = this;
    that.setData({
      topload: true,
      loadmoreShow: false,
      isLastPage: false,
      page: 1,
    });
    that.getShopBanner();
    that.getShopNotice();
    that.getShopPlate();
    that.getShopInspiration();
    that.getShopHotList();
    that.getShopIndexRecommend();
    wx.stopPullDownRefresh();
  },

  onReachBottom: function () {
    let that = this;
    that.setData({
      loadmoreShow: true,
      page: that.data.page + 1
    });
    that.getShopIndexRecommend();
  },

  onShareAppMessage: function (res) {
    let that = this;
    if (res.from == "button") {
      setTimeout(function () {
        that.freeGetVip();
      }, 2500)
      return {
        title: '邀你一起免费领取会员啦！',
        path: '/pages/tabbar/index/index',
        imageUrl: 'https://yuebudao-1307721138.cos.ap-shanghai.myqcloud.com/2023/01/08/df7a34ba76dceda874e70c14e2a3a462.png',
      }
    }
  },
}

shop(options)
common(options)
Page(options)